package edu.xcdq;

import sun.awt.geom.AreaOp;

import java.util.Arrays;
import java.util.Scanner;

/**
 *商品管理员
 */
public class ArticleManage {
    //创建一个商品集合（仓库）实例
    ArticleSet articleSet =  new  ArticleSet();

    //对新建的仓库进行初始化
    public void initial(){
        //创建商品
        Article xiaomi11=new Article();
        xiaomi11.setArticle("小米11",50,2999,1);

       Article xiaomi11Pro = new Article();
        xiaomi11Pro.setArticle("小米11专业版",30,3999,2);

        Article xiaomiULtra = new Article();
        xiaomiULtra.setArticle("小米增强版",66,4999,3);
        //把商品放到仓库中
        articleSet.articles[0]=xiaomi11;
        articleSet.articles[1]=xiaomi11Pro;
        articleSet.articles[2]=xiaomiULtra;

    }
    //起始菜单
    public  void  startMenu() {
        boolean flag = true;
        do {
            System.out.println("*********************************");
            System.out.println("1.查看商品信息");
            System.out.println("2.新增商品信息");
            System.out.println("3.删除商品信息");
            System.out.println("4.卖出商品信息");
            System.out.println("5.商品销售排行榜");
            System.out.println("6.退出系统");
            System.out.println("***********************************");
            System.out.println("请输入你要执行的操纵:");
            Scanner scanner = new Scanner(System.in);
            int gongnengbianhao = scanner.nextInt();
            switch (gongnengbianhao) {
                case 1:
                    System.out.println("查看商品信息");
                    chakan();
                    break;
                case 2:
                    System.out.println("新增商品信息");
                    add();
                    break;
                case 3:
                    System.out.println("删除商品信息");
                    delete();
                    break;
                case 4:
                    System.out.println("卖出商品");
                    sell();
                    break;
                case 5:
                    System.out.println("商品销售排行榜");
                    paihangbang();
                    break;
                case 6:
                    System.out.println("退出系统");
                    exit();
                    flag = false;
                    break;
                default:
                    System.out.println("你输入的功能编号有错");
                    break;
            }

        } while (flag);
    }
    public  void  exit(){
        System.out.println("谢谢，感谢使用！已经退出");
    }
    public void paihangbang(){
        //冒泡排序
        for (int i = 0 ; i <articleSet.articles.length-1;i++){
            for (int j= 0;j <articleSet.articles.length-i-1;j++){
                //如果当前的元素值比后面的元数值小，就与后面的位置交换
                if (articleSet.articles[j]!=null&&articleSet.articles[j+1]!=null){
                if (articleSet.articles[j].number<articleSet.articles[j+1].number){
                    //交换位置
             Article  tempArticle = articleSet.articles[j];
             articleSet.articles[j] = articleSet.articles[j+1];
             articleSet.articles[j+1]=tempArticle;

                }
            }
        }
        }
        System .out.println("****************排行榜**************************");
        System.out.println("名词\t销售数量\t商品名称");
        for (int i = 0 ; i < articleSet.articles.length;i++){
            if (articleSet.articles[i]!=null){
            System.out.println((i+1)+"\t"+articleSet.articles[i].number+"\t"+articleSet.articles[i].name);
        }
    }
    }
    //卖出商品
    public  void sell(){
        System.out.println("请问你要卖出的名称：");
        Scanner scanner = new Scanner(System.in);
        String  name = scanner.next();
        for (int i = 0 ; i <articleSet.articles.length; i ++){
            if (articleSet.articles[i].name.equals(name)){
                System.out.println("数量：");
                int num = scanner.nextInt();
                if (num<articleSet.articles[i].amount){
                    articleSet.articles[i].amount =articleSet.articles[i].amount-num;
                    articleSet.articles[i].number = articleSet.articles[i].number+num;
                }
                System.out.println("成功卖出！");
                break;
            }
        }
    }
    //删除商品信息
    public void  delete(){
        System.out.println("请输入你要删除的商品编号：");
        Scanner scanner = new Scanner(System.in);
       int  delNo = scanner.nextInt();
       for (int i = 0 ; i <articleSet.articles.length;i++){
           if (articleSet.articles[i]!=null && (i+1)==delNo){   //数组中的元素存在&&  找到要删除的元素i
               int  j = i ;
               while(articleSet.articles[j+1]!=null){    //要移动的元素后一个位置不为空，执行以下操作
                   articleSet.articles[j]=articleSet.articles[j+1];//把后面元素的值覆盖在前面的元素上
               j++;
               }
               articleSet.articles[i]=null;//最后一个元素手动修改为空
               System.out.println("删除成功！");
           }else {
               System.out.println("删除失败！");
           }
       }

    }

    //查看商品信息
    public void chakan(){
        System.out.println("编号：\t名称\t价格\t库存\t销售数量");
        for (int  i = 0 ; i <articleSet.articles.length;i++){
            if (articleSet.articles[i]!=null){
                articleSet.articles[i].print(i+1);
                //只添加数组中的第一个空位置，后续的空位置直接跳过，中断循环
            }
        }
    }

    //添加商品
    public void add(){
        System.out.println("请输入商品名称：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        System.out.println("输入价格：");
        double price = scanner.nextDouble();
        System.out.println("输入库存：");
        int  kucun =scanner.nextInt();
        System.out.println("已卖出数量：");
        int maichu  =scanner.nextInt();


        Article newArticle  =new Article();
        newArticle.setArticle(name, kucun,price, maichu);

        for (int i = 0 ; i<articleSet.articles.length;i++){
            if (articleSet.articles[i]==null){
                articleSet.articles[i]=  newArticle;
                break; //
            }
        }








    }
}
